package GUI;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;

public class GamePanel extends JPanel {

//    private GameApplet ga;
    private Game game;
    private boolean paused = false;
    UpdateThread thread;
    //the time in miliseconds between every repaint.
    private int sleeptime = 40;
    public JButton button = new JButton();

    public GamePanel(Game g) {
        game = g;
        addMouseListener(new MouseAdapter() {

            @Override
            public void mousePressed(MouseEvent e) {
                game.mousePressed(e);
            }

            @Override
            public void mouseClicked(MouseEvent e) {
                game.mouseClicked(e);
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                game.mouseReleased(e);
            }

            @Override
            public void mouseEntered(MouseEvent e) {
                game.mouseEntered(e);
            }

            @Override
            public void mouseExited(MouseEvent e) {
                game.mouseExited(e);
            }
        });

        addKeyListener(new KeyAdapter() {

            @Override
            public void keyPressed(KeyEvent e) {
                game.keyPressed(e);
            }

            @Override
            public void keyTyped(KeyEvent e) {
                game.keyTyped(e);
            }

            @Override
            public void keyReleased(KeyEvent e) {
                game.keyReleased(e);
            }
        });


        this.setLayout(null);
        ImageIcon icon = new ImageIcon(ImageUtility.loadImageFromResource_("Images/pass_arrow_small.png"));
        button.setIcon(icon);
        button.setVisible(true);
        this.add(button);
        button.setMargin(new Insets(0, 0, 0, 0));
        button.setBounds(500, 360, 54, 32);
        this.validate();

        this.button.addActionListener(this.game);
    }

    public void setPause(boolean p) {
        paused = p;
    }

    public void start() {
        thread = new UpdateThread(this);
        thread.start();
        this.paused = false;
    }

    public void setSleepTime(int ms) {
        sleeptime = ms;
    }

    public void stop() {
        game.stop();
    }

    public boolean isPaused() {
        return paused;
    }

    @Override
    public void paint(Graphics g) {
//        System.out.println(Thread.currentThread().getId());

        //Fills everything with background color.
        //You might want to skip this part if you do not need to clear the panel every frame
        //To do this, add an abstract method to Game, called "public boolean clearPanel()" that
        //will tell whether the panel should be cleared.
        Color c = game.getBackgroundColor();
        if (c != null) {
            g.setColor(c);
            g.fillRect(0, 0, getWidth(), getHeight());
        }
        game.paintGame(g);
        this.paintComponents(g);
    }

    public void showPassButton(boolean flag) {
        if (flag != this.button.isVisible()) {
            this.button.setVisible(flag);
        }
    }

    public void enablePassButton(boolean flag) {
        if (flag != this.button.isEnabled()) {
            this.button.setEnabled(flag);
        }
    }

    public boolean isPassButtonEnable() {
        return this.button.isEnabled();
    }

    public boolean isPassButtonVisible() {
        return this.button.isVisible();
    }

    class UpdateThread extends Thread {

        private GamePanel gp;

        UpdateThread(GamePanel g) {
            gp = g;
        }

        @Override
        public void run() {
            while (true) {
                try {
                    sleep(sleeptime);
                } catch (Exception e) {
                    e.printStackTrace(System.err);
                }
                if (!gp.isPaused()) {
                    gp.game.updateGame();
                    gp.repaint();
                }
            }
        }
    }
}
